package com.sgb.kjwl.view.ui.activity.material;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.alimaplibrary.MapLocationTools;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityStList3Binding;
import com.sgb.kjwl.di.material.DaggerMaterialComponent;
import com.sgb.kjwl.di.material.MaterialModule;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.event.CategoryEvent;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.MaterialEvent;
import com.sgb.alimaplibrary.SelectAddressActivity;
import com.sgb.kjwl.view.ui.activity.home.HomeCategoryActivity;
import com.sgb.kjwl.view.ui.fragment.material.STListFragment3;
import com.sgb.kjwl.viewmodel.material.CommercialConcreteListModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.MLog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 商砼列表
 */
public class CommercialConcreteListActivity3 extends AppActivity {

    @Inject
    CommercialConcreteListModel3 viewModel;
    private ActivityStList3Binding binding;
    private double locCurrentLat = 0;
    private double locCurrentLong = 0;

    private String title;
    private String id;
    private String fId;  //用户筛选类别查询
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private String[] titles;
    private MapLocationTools mapLocationTools;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMaterialComponent.builder().appComponent(appComponent).materialModule(new MaterialModule(this)).build().inject(this);
    }

    public static void start(Context context, String title, String id, String mId) {
        Intent intent = new Intent();
        intent.putExtra("title", title);
        intent.putExtra("id", id);
        intent.putExtra("mId", mId);
        intent.setClass(context, CommercialConcreteListActivity3.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_st_list3);
        binding.ivBack.setVisibility(View.VISIBLE);
        binding.ivBack.setOnClickListener(this);
        binding.rightView.setOnClickListener(this);
        binding.locView.setOnClickListener(this);

        binding.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    closeKeybord();
                    EventBus.getDefault().postSticky(MaterialEvent.make().setMessageTag(EventConstant.EVENT_ST_FILTER).setMessageData(binding.editSearch.getText().toString()));
                }
                return false;
            }
        });
    }

    @Override
    protected void init() {
        title = getIntent().getStringExtra("title");
        id = getIntent().getStringExtra("id");
        fId = getIntent().getStringExtra("mId");
        startLocation();
        binding.tvTitle.setText(title);
        viewModel.materialFilterJson.get().setItemCid(fId);
        viewModel.materialFilterJson.get().setItemId(id);
        viewModel.getFilterTypeModel(fId);
    }

    public void showTypeView(List<DictEntity3> datas) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        mFragments = new ArrayList<>();
        int size = datas.size();
        titles = new String[size];
        for (int i = 0; i < size; i++) {
            mFragments.add(STListFragment3.newInstance(id, fId,i));
            titles[i] = datas.get(i).getName();
        }
        pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(titles.length - 1);
        binding.tablayout.setViewPager(binding.viewpager, titles);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
                EventBus.getDefault().postSticky(CategoryEvent.make(EventConstant.EVENT_CATEGORY_CHOOSE, 0));
                HomeCategoryActivity.start(getActivity());
                break;
            case R.id.loc_view:
                Intent intent = new Intent();
                intent.setClass(CommercialConcreteListActivity3.this, SelectAddressActivity.class);
                intent.putExtra(SelectAddressActivity.TITLE, "选择定位地址");
                intent.putExtra(SelectAddressActivity.LATITUDE, locCurrentLat);
                intent.putExtra(SelectAddressActivity.LONGTITUDE, locCurrentLong);
                startActivityForResult(intent, 101);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && data != null) {
            locCurrentLat = data.getDoubleExtra(SelectAddressActivity.LATITUDE, 0);
            locCurrentLong = data.getDoubleExtra(SelectAddressActivity.LONGTITUDE, 0);
            EventBus.getDefault().post(MaterialEvent.make().setMessageTag(EventConstant.EVENT_GOODS_LOC_SELECT).setMessageData(locCurrentLat+"").setObject(locCurrentLong+""));
        }
    }

    /**
     * 开始定位
     */
    private void startLocation() {
        if (mapLocationTools == null) {
            mapLocationTools = new MapLocationTools(getApplicationContext());
            mapLocationTools.initLocation(false);
        }
        mapLocationTools.setLocationListener(new MapLocationTools.LocationListener() {
            @Override
            public void getLocationListener(String address, Double Longitude, Double Latitude) {
                locCurrentLat = Latitude;    //获取纬度信息
                locCurrentLong = Longitude;    //获取经度信息
                EventBus.getDefault().postSticky(MaterialEvent.make().setMessageTag(EventConstant.EVENT_GOODS_LOC).setMessageData(locCurrentLat+"").setObject(locCurrentLong+""));

                //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准
                MLog.d("test", "locCurrentLat " + locCurrentLat);
                MLog.d("test", "locCurrentLong " + locCurrentLong);
            }
        });
        mapLocationTools.startLocation();
    }
}
